export interface IUtilsOption {
  name?: string;
  nameStyle?: string;
}
export interface IPrintOption {
  style1?: string;
  style2?: string;
}

export type TAlarmLevel = "debug" | "info" | "warn" | "error" | "success";

enum E_levelColor {
  "debug" = "#666",
  "info" = "#006EFF",
  "warn" = "#FF9D00",
  "success" = "#0ABF5B",
  "error" = "#E54545",
}

class LogUtils {
  private _NAME = "";
  private _nameStyle = ""

  constructor(utilsOption?: IUtilsOption) {
    this._NAME = utilsOption?.name || "xc logUtils";
    this._nameStyle = utilsOption?.nameStyle || "background:linear-gradient(to right, #35495e , #41b883); border-radius:3px; color:#fff; padding: 2px 4px; margin-right:4px;";
  }

  private _print(
    cnt: string | Object,
    cnt2: string | Object,
    printOption: IPrintOption
  ) {
    cnt =
      typeof cnt === "object" ? `\n${JSON.stringify(cnt, undefined, 2)}` : cnt;
    cnt2 =
      typeof cnt2 === "object"
        ? `\n${JSON.stringify(cnt2, undefined, 2)}`
        : cnt2;
    console.log(
      `%c ${this._NAME} %c ${cnt} %c ${cnt2} `,
      this._nameStyle,
      printOption.style1,
      printOption.style2
    );
  }

  private _fontColorPrint(
    cnt: string | Object,
    cnt2: string | Object = "",
    alarmLevel: TAlarmLevel
  ) {
    this._print(cnt, cnt2, {
      style1: `color:${E_levelColor[alarmLevel]}; padding:2px 4px;`,
      style2: `color:${E_levelColor[alarmLevel]}; padding:2px 4px;`,
    });
  }

  info(cnt: string | Object, cnt2: string | Object = "") {
    this._fontColorPrint(cnt, cnt2, "info");
  }
  warn(cnt: string | Object, cnt2: string | Object = "") {
    this._fontColorPrint(cnt, cnt2, "warn");
  }
  error(cnt: string | Object, cnt2: string | Object = "") {
    this._fontColorPrint(cnt, cnt2, "error");
  }
  success(cnt: string | Object, cnt2: string | Object = "") {
    this._fontColorPrint(cnt, cnt2, "success");
  }

  private _bgColorPrint(
    cnt: string | Object,
    cnt2: string | Object = "",
    alarmLevel: TAlarmLevel
  ) {
    this._print(cnt, cnt2, {
      style1: `background-color:${E_levelColor[alarmLevel]}; padding:2px 4px; color:#fff; border-radius:4px; margin-right:4px;`,
      style2: `background-color:${E_levelColor[alarmLevel]}; padding:2px 4px; color:#fff; border-radius:4px; margin-right:4px;`,
    });
  }

  bgInfo(cnt: string | Object, cnt2: string | Object = "") {
    this._bgColorPrint(cnt, cnt2, "info");
  }
  bgWarn(cnt: string | Object, cnt2: string | Object = "") {
    this._bgColorPrint(cnt, cnt2, "warn");
  }
  bgError(cnt: string | Object, cnt2: string | Object = "") {
    this._bgColorPrint(cnt, cnt2, "error");
  }
  bgSuccess(cnt: string | Object, cnt2: string | Object = "") {
    this._bgColorPrint(cnt, cnt2, "success");
  }

  private _mixPrint(
    cnt: string | Object,
    cnt2: string | Object = "",
    alarmLevel: TAlarmLevel
  ) {
    this._print(cnt, cnt2, {
      style1: `background-color:${E_levelColor[alarmLevel]}; padding:2px 4px; color:#fff; border-radius:4px; margin-right:4px;`,
      style2: `color:#666; padding:2px 4px;`,
    });
  }

  mixInfo(cnt: string | Object, cnt2: string | Object = "") {
    this._mixPrint(cnt, cnt2, "info")
  }
  mixWarn(cnt: string | Object, cnt2: string | Object = "") {
    this._mixPrint(cnt, cnt2, "warn")
  }
  mixError(cnt: string | Object, cnt2: string | Object = "") {
    this._mixPrint(cnt, cnt2, "error")
  }
  mixSuccess(cnt: string | Object, cnt2: string | Object = "") {
    this._mixPrint(cnt, cnt2, "success")
  }
}

export default LogUtils;
